<?php

class UrlruleAction extends PublicAction
{
    protected $urlrule;

    function _initialize()
    {
        parent::_initialize();
        $this->urlruleModel = D('Urlrule');
    }

    public function index()
    {
        if (empty($_REQUEST['where'])) {
            $list = $this->urlruleModel->select();
        } else {
            $list = $this->urlruleModel->where($_REQUEST['where'])->select();
        }

        $this->assign('list', $list);
        $this->display();
    }

    function update()
    {
        if ($_POST['setup'])
            $_POST['setup'] = array2string($_POST['setup']);

        if (false === $this->urlruleModel->create()) {
            $this->error($this->urlruleModel->getError());
        }

        if (false !== $this->urlruleModel->save()) {

            saveCache('Urlrule');
            $this->success('修改成功!', cookie('__forward__'));
        } else {
            $this->success('修改失败!: ' . $this->urlruleModel->getDbError());
        }
    }

    function add()
    {
        $this->display('edit');
    }

    function edit()
    {
        $pk = ucfirst($this->urlruleModel->getPk());
        $id = $_REQUEST[$this->urlruleModel->getPk()];

        if (empty($id))
            $this->error('缺少必要的参数!');

        $do = 'getBy' . $pk;
        $vo = $this->urlruleModel->$do($id);

        if ($vo['setup'])
            $vo['setup'] = string2array($vo['setup']);

        $this->assign('vo', $vo);
        $this->display();
    }
}